const state = {
  visitedViews: [],
}

const mutations = {
  ADD_VIEW(state, view) {
    if (view.path && view.meta.title) {
      const data = {
        path: view.path,
        title: view.meta.title
      }
      const flag = JSON.stringify(state.visitedViews).includes(JSON.stringify(data))
      if (flag) {
        for (let i = 0; i < state.visitedViews.length; i++) {
          if (state.visitedViews[i].path == data.path) {
            state.visitedViews.splice(i, 1)
            state.visitedViews.push(data)
            return
          }
        }
      } else {
        state.visitedViews.push(data)
      }

    }
  },
  DELETE_VIEW(state, path) {
    for (let i = 0; i < state.visitedViews.length; i++) {
      if (state.visitedViews[i].path == path) {
        state.visitedViews.splice(i, 1)
        return
      }
    }
  }

}

const actions = {
  addView({ commit }, view) {
    commit('ADD_VIEW', view)
  },
  deleteView({ commit }, path) {
    commit('DELETE_VIEW', path)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
